package com.miao.util.base.dao.impl;

import com.miao.util.annotation.Cache;
import com.miao.util.annotation.Flush;
import com.miao.util.base.dao.Dao;
import com.miao.util.base.pojo.Basic;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Created by Miaosk
 * @date Created on 2015/5/19.
 */
@Service
public class DaoImpl<T> implements Dao<T> {

    @Resource
    protected SqlSession sqlSession;

    @Override
    @Flush
    public int save(T t) {
        String model = getIface();
        if (t instanceof Basic) {
            Basic basic = (Basic) t;
            int num = isExist(basic.getUuid());
            {
                if (num == 0) {
                    num = saveBasic(basic);
                }
                if (num != 1) {
                    throw new RuntimeException("erro save in obj basic");
                }
            }
        } else {
            System.out.println(t.getClass() + " is not instanceof Basic");
        }
        int i = sqlSession.insert(model + ".save", t);
        return i;
    }

    @Override
    @Flush
    public int delete(Map map) {
        String model = getIface();
        return sqlSession.delete(model + ".delete", map);
    }

    @Cache(expiration = 1 * 60)
    public T findById(int id) {
        String model = getIface();
        return (T) sqlSession.selectOne(model + ".findById", id);
    }

    @Override
    public T findByUUID(String uuid) {
        String model = getIface();
        return (T) sqlSession.selectOne(model + ".findByUUID", uuid);
    }

    @Override
    @Cache(expiration = 1 * 60)
    public T findByName(String name) {
        String model = getIface();
        return (T) sqlSession.selectOne(model + ".findByName", name);
    }

    @Override
    @Cache(expiration = 1 * 60)
    public List<T> list(Map map) {
        String model = getIface();
        return sqlSession.selectList(model + ".list", map);
    }

    @Cache(expiration = 1 * 60)
    public int count(Map map) {
        String model = getIface();
        return (Integer) sqlSession.selectOne(model + ".count", map);
    }

    @Flush
    public int deleteById(int id) {
        String model = getIface();
        return sqlSession.delete(model + ".deleteById", id);
    }

    @Override
    public int deleteByUUID(String uuid) {
        String model = getIface();
        return sqlSession.delete(model + ".deleteByUUID", uuid);
    }

    @Flush
    public int update(T t) {
        String model = getIface();
        return sqlSession.update(model + ".update", t);
    }

    @Override
    @Cache(expiration = 1 * 60)
    public T find(Map map) {
        String model = getIface();
        return (T) sqlSession.selectOne(model + ".find", map);
    }

    @Override
    @Flush
    public int deleteBunch(int[] id) {
        String model = getIface();
        return sqlSession.update(model + ".deleteBunch", id);
    }

    protected String getIface() {
        return this.getClass().getInterfaces()[0].getName();
    }

    private int saveBasic(Basic t) {
        int i = 0;
        i = sqlSession.insert(Dao.class.getName() + ".saveBasic", t);
        if (i != 1) {
            throw new RuntimeException("erro save in obj basic");
        }
        return i;
    }

    private int isExist(String uuid) {
        int i = 0;
        i = (Integer) sqlSession.selectOne(Dao.class.getName() + ".isExist", uuid);
        return i;
    }
}
